Type.registerNamespace('ModWatch');

ModWatch.OnRadioButtonChangedModAlertBehavior = function(element) { 
    ModWatch.OnRadioButtonChangedModAlertBehavior.initializeBase(this, [element]);

    this._groupName = null;
    this._isSelected = false;
	this._previousOnClickEvent = null;
}

ModWatch.OnRadioButtonChangedModAlertBehavior.prototype =
{

	get_groupName : function()
	{
		return this._groupName;
	},

	set_groupName : function(value)
	{
		this._groupName = value;
	},

	_onClick : function(e)
	{
		if (!ModWatch.OnRadioButtonChangedModAlertBehavior.callBaseMethod(this, 'verifyChanges'))
		{
			// roll back the change
			ModWatch.ModAlertRadioButtonTrackerInstance.revertToSavedValues(this, this.get_groupName());
			return false;
		}
		else
		{
			// store the new selected index
			ModWatch.ModAlertRadioButtonTrackerInstance.saveCurrentValues(this, this.get_groupName());

			if (this._previousOnClickEvent != null)
			{
				return this._previousOnClickEvent(e);
			}
		}
	},

	initialize : function()
	{
		ModWatch.OnRadioButtonChangedModAlertBehavior.callBaseMethod(this, 'initialize');

		var rdo = this.get_element();
		var currentChangeEvent = rdo.onclick;
		if (typeof currentChangeEvent == 'function')
		{
			this._previousOnClickEvent = currentChangeEvent;
		}

		this.saveCurrentValue();

		ModWatch.ModAlertRadioButtonTrackerInstance.addRadioButton(this, this.get_groupName());

		$addHandlers(rdo, 
					 { 'click' : this._onClick },
					 this);

		rdo.onclick = null;
	},

	dispose : function()
	{
		ModWatch.ModAlertRadioButtonTrackerInstance.removeRadioButton(this, this.get_groupName());

		this.get_element().onclick = this._previousOnClickEvent;

		$clearHandlers(this.get_element());

		ModWatch.OnRadioButtonChangedModAlertBehavior.callBaseMethod(this, 'dispose');
	},

	revertToSavedValue : function()
	{
		this.get_element().checked = this._isSelected;
	},

	saveCurrentValue : function()
	{
		this._isSelected = this.get_element().checked;
	}

}

ModWatch.OnRadioButtonChangedModAlertBehavior.registerClass('ModWatch.OnRadioButtonChangedModAlertBehavior', ModWatch.ModAlertBehaviorBase);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
